package cn.maszy.action.projectmanagement;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sourceforge.stripes.action.SessionScope;
import org.springframework.beans.factory.annotation.Autowired;
import cn.maszy.common.BaseAction;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.common.Transcoding;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.systemsetting.B_T_Ufp_Material;
import cn.maszy.model.systemsetting.B_T_Ufp_Project;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerCategoryService;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineeringService;
import cn.maszy.service.systemsetting.B_T_Ufp_ProjectService;

@SessionScope
public class EngineerCategoryAction extends BaseAction<B_T_Ufp_EngineerCategory> {
	/**
	 * 版本控制信息
	 */
	private static final long serialVersionUID = 6179604301571294802L;
	private B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory;
	//自定义项目添加的时候用
	private B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategoryCustom;
	private B_T_Ufp_Project b_T_Ufp_Project;
	private List<B_T_Ufp_Project> list_B_T_Ufp_Project;
	@Autowired
	private B_T_Ufp_ProjectService b_T_Ufp_ProjectService;
	@Autowired
	private B_T_Ufp_EngineerCategoryService b_T_Ufp_EngineerCategoryService;
	@Autowired
	private B_T_Ufp_EngineeringService b_T_Ufp_EngineeringService;
	private String checkIds;
	private String flag;
	private String categoryStatus;
	//总金额
	private Float totalamount;
	//用于传递我的工程时 给具体部位增加数据时，传递uuid和那么给他的子类。
	private String positiontype;
	private JSONArray data;
	//是否可以修改价格
	private String isupdate;
	/*******************STRUTS ACTION METHOD*************************/
	
	//删除session中的数据
		public String engineerCategoryRemove() throws Exception
		{
			list_B_T_Ufp_Project = (List<B_T_Ufp_Project>) request.getSession().getAttribute("item");
			if(list_B_T_Ufp_Project!=null)
			{
				for(int i=list_B_T_Ufp_Project.size();i>0;i--)
				{
					if(list_B_T_Ufp_Project.get(i-1).getProjectName().equals(checkIds))
					{
						list_B_T_Ufp_Project.remove(i-1);
					}
				}
			}
			return "success";
		}
		 //获取session值并传递到页面js中进行设置
		public String getsesssion() throws Exception{
			list_B_T_Ufp_Project = (List<B_T_Ufp_Project>) request.getSession().getAttribute("item");
			if(list_B_T_Ufp_Project!=null)
			{
				data = JSONArray.fromObject(list_B_T_Ufp_Project); 
//				data = jsonArray.toString();
			}
//			request.setAttribute("data",jsonBytes);
			return SUCCESS;
		}
 	public String engineerCategorysetsession()throws Exception{
		if(b_T_Ufp_Project!=null)
		{			
			list_B_T_Ufp_Project = (List<B_T_Ufp_Project>) request.getSession().getAttribute("item");
			if(list_B_T_Ufp_Project ==null){
				list_B_T_Ufp_Project = new ArrayList<B_T_Ufp_Project>();
			}
			
			//添加材料的时候不能重复添加根据项目编号
			for(int i=0;i<list_B_T_Ufp_Project.size();i++)
			{
				if(b_T_Ufp_Project.getProjectNum().equals(list_B_T_Ufp_Project.get(i).getProjectNum()))
				{	//先请除session
					request.getSession().removeAttribute("Materialitem");
					float j =list_B_T_Ufp_Project.get(i).getQuantities()+b_T_Ufp_Project.getQuantities();
					String s =list_B_T_Ufp_Project.get(i).getSpare();
					String k =Float.parseFloat(s)+b_T_Ufp_Project.getQuantities()*b_T_Ufp_Project.getTotal()+"";
					list_B_T_Ufp_Project.get(i).setQuantities(j);
					list_B_T_Ufp_Project.get(i).setSpare(k);
					
					//再把设置过的值放入session
					request.getSession().setAttribute("item", list_B_T_Ufp_Project);
					return ajaxForwardSuccess(getText("msg.operation.success"), "","tttt", "closeCurrent", "");
				}
			}
				if(positiontype!=null&&!"".equals(positiontype))
				{
//					positiontype =Transcoding.getcode(request, positiontype);
//					positiontype =new String(positiontype.getBytes("ISO8859_1"),"GBK");
					String flag1[]=positiontype.split(",");
					//这里占时借用这3个字段来存储工程部位id，name 工程uuid
					b_T_Ufp_Project.setCategoryUuid(flag1[0]);
					b_T_Ufp_Project.setCategoryName(flag1[1]);
					b_T_Ufp_Project.setSchreiben(flag1[2]);
				}
				float s =b_T_Ufp_Project.getTotal()*b_T_Ufp_Project.getQuantities();
				String ss=s+"";
				b_T_Ufp_Project.setSpare(ss);
				list_B_T_Ufp_Project.add(b_T_Ufp_Project);
				request.getSession().setAttribute("item", list_B_T_Ufp_Project);
		}
		
		return ajaxForwardSuccess(getText("msg.operation.success"), "","tttt", "closeCurrent", "");
	}
	//删除
	public String engineerCategoryDelete() throws Exception {
		int i=b_T_Ufp_EngineerCategoryService.delete(checkIds);
		if(i>0)
		return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "", "");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	//修改新增
	public String engineerCategoryUpdate() throws Exception {
		//保存时获取session中设置的值，
		list_B_T_Ufp_Project = (List<B_T_Ufp_Project>) request.getSession().getAttribute("item");
		if(list_B_T_Ufp_Project!=null)
		{
			int j=0;
			for(int i=0;i<list_B_T_Ufp_Project.size();i++)
			{
				b_T_Ufp_Project=list_B_T_Ufp_Project.get(i);
				b_T_Ufp_EngineerCategory = new B_T_Ufp_EngineerCategory();
				b_T_Ufp_EngineerCategory.setProjectNum(b_T_Ufp_Project.getProjectNum());
				b_T_Ufp_EngineerCategory.setPositionUuid(b_T_Ufp_Project.getCategoryUuid());
				b_T_Ufp_EngineerCategory.setPositionName(b_T_Ufp_Project.getCategoryName());
				b_T_Ufp_EngineerCategory.setProjectName(b_T_Ufp_Project.getProjectName());
				b_T_Ufp_EngineerCategory.setFinishes(b_T_Ufp_Project.getFinishes());
				b_T_Ufp_EngineerCategory.setUnit(b_T_Ufp_Project.getUnit());
				b_T_Ufp_EngineerCategory.setLaborCost(b_T_Ufp_Project.getLaborCost());
				b_T_Ufp_EngineerCategory.setMaterialCost(b_T_Ufp_Project.getMaterialCost());
				b_T_Ufp_EngineerCategory.setTotal(b_T_Ufp_Project.getTotal());
				b_T_Ufp_EngineerCategory.setQuantities(b_T_Ufp_Project.getQuantities());
				b_T_Ufp_EngineerCategory.setRemark(b_T_Ufp_Project.getRemark());
				b_T_Ufp_EngineerCategory.setEngineeringUuid(b_T_Ufp_Project.getSchreiben());
				float s =b_T_Ufp_Project.getTotal()*b_T_Ufp_Project.getQuantities();
				b_T_Ufp_EngineerCategory.setTotalAmount(s);
				j=b_T_Ufp_EngineerCategoryService.update(b_T_Ufp_EngineerCategory);
				if(j>0)
				{
					//查询 这条数据所属的工程 的项目金额总和
					B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory = new B_T_Ufp_EngineerCategory();
					b_T_Ufp_EngineerCategory.setEngineeringUuid(b_T_Ufp_Project.getSchreiben());
					float price =b_T_Ufp_EngineerCategoryService.totalall(b_T_Ufp_EngineerCategory);
					//设置参数并修改工程 的项目总价格
					B_T_Ufp_Engineering b_T_Ufp_Engineering=new B_T_Ufp_Engineering();
					b_T_Ufp_Engineering.setUuid(b_T_Ufp_Project.getSchreiben());
					b_T_Ufp_Engineering.setEngineeringPrice(price);
					b_T_Ufp_EngineeringService.update(b_T_Ufp_Engineering);
				}
			}
			if(j>0)
			//目前只有点击提交后关闭
				//保存成功清除session
			request.getSession().removeAttribute("item");
			return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "closeCurrent", "");
		}
		else
		{
			if(b_T_Ufp_EngineerCategory ==null)
			{
				return ajaxForwardError(getText("请填写数据后保存"));
			}
			int i=b_T_Ufp_EngineerCategoryService.update(b_T_Ufp_EngineerCategory);
			if(i>0)
			{	
				float price =b_T_Ufp_EngineerCategoryService.totalall(b_T_Ufp_EngineerCategory);
				//设置参数并修改工程 的项目总价格
				B_T_Ufp_Engineering b_T_Ufp_Engineering=new B_T_Ufp_Engineering();
				b_T_Ufp_Engineering.setUuid(b_T_Ufp_EngineerCategory.getEngineeringUuid());
				b_T_Ufp_Engineering.setEngineeringPrice(price);
				b_T_Ufp_EngineeringService.update(b_T_Ufp_Engineering);
				return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "closeCurrent", "");
			}
		}
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	
	public String engineerCategoryList() throws Exception {
		//查询所属部位的合计金额
		totalamount = b_T_Ufp_EngineerCategoryService.totalamount(b_T_Ufp_EngineerCategory);
		//页面传值转码
		if(b_T_Ufp_EngineerCategory.getPositionName()!=null)
		{
			String s = b_T_Ufp_EngineerCategory.getPositionName();
			s =Transcoding.getcode(request, s);
//			s=new String(s.getBytes("ISO8859_1"),"GBK");
			b_T_Ufp_EngineerCategory.setPositionName(s);
		}
		pager = new Pager<B_T_Ufp_EngineerCategory>(pageNum, numPerPage,orderField,orderDirection);
		pager= b_T_Ufp_EngineerCategoryService.selectList(b_T_Ufp_EngineerCategory,pager);
		return SUCCESS;
	}
	
	public String engineerCategoryDetail() throws Exception {
		if(positiontype!=null&&!"".equals(positiontype))
		{
//			positiontype =new String(positiontype.getBytes("ISO8859_1"),"GBK");
			positiontype =Transcoding.getcode(request, positiontype);
		}
		//当添加工程量的时候调用工程量占用spare字段。
		if(b_T_Ufp_Project!=null)
		{
			b_T_Ufp_Project =b_T_Ufp_ProjectService.selectOne(b_T_Ufp_Project.getUuid());
			return SUCCESS;
		}
		if(b_T_Ufp_EngineerCategory==null){
			b_T_Ufp_EngineerCategory=b_T_Ufp_EngineerCategoryService.selectOne("");
		}else{
			// 自定义添加项目首先记住传过来的工程uuid
			String engineeringuuid = b_T_Ufp_EngineerCategory.getEngineeringUuid();
			
			b_T_Ufp_EngineerCategory=b_T_Ufp_EngineerCategoryService.selectOne(b_T_Ufp_EngineerCategory.getUuid());
			
			if(engineeringuuid!=null&&!"".equals(engineeringuuid))
			{
				String s[]=positiontype.split(",");
				b_T_Ufp_EngineerCategoryCustom = new B_T_Ufp_EngineerCategory();
				b_T_Ufp_EngineerCategoryCustom.setPositionUuid(s[0]);
				b_T_Ufp_EngineerCategoryCustom.setPositionName(s[1]);
				//自动项目编码
				String str = new SimpleDateFormat("MMddHHmmss").format(new java.util.Date());
				String code ="XM_"+str+SessionFactory.getSessionUserName();
				b_T_Ufp_EngineerCategoryCustom.setProjectNum(code);
				b_T_Ufp_EngineerCategoryCustom.setEngineeringUuid(engineeringuuid);
			}
			
		}
		return SUCCESS;
	}
	
	
	/********************* class set/get ****************************/
	
	public B_T_Ufp_EngineerCategory getB_T_Ufp_EngineerCategory() {
		return b_T_Ufp_EngineerCategory;
	}

	public void setB_T_Ufp_EngineerCategory(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory) {
		this.b_T_Ufp_EngineerCategory = b_T_Ufp_EngineerCategory;
	}
	
	public String getCheckIds() {
		return checkIds;
	}

	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getCategoryStatus() {
		return categoryStatus;
	}
	public void setCategoryStatus(String categoryStatus) {
		this.categoryStatus = categoryStatus;
	}
	public B_T_Ufp_Project getB_T_Ufp_Project() {
		return b_T_Ufp_Project;
	}
	public void setB_T_Ufp_Project(B_T_Ufp_Project b_T_Ufp_Project) {
		this.b_T_Ufp_Project = b_T_Ufp_Project;
	}
	public List<B_T_Ufp_Project> getList_B_T_Ufp_Project() {
		return list_B_T_Ufp_Project;
	}
	public void setList_B_T_Ufp_Project(List<B_T_Ufp_Project> list_B_T_Ufp_Project) {
		this.list_B_T_Ufp_Project = list_B_T_Ufp_Project;
	}
	public String getPositiontype() {
		return positiontype;
	}
	public void setPositiontype(String positiontype) {
		this.positiontype = positiontype;
	}
	public Float getTotalamount() {
		return totalamount;
	}
	public void setTotalamount(Float totalamount) {
		this.totalamount = totalamount;
	}
	public JSONArray getData() {
		return data;
	}
	public void setData(JSONArray data) {
		this.data = data;
	}
	public B_T_Ufp_EngineerCategory getB_T_Ufp_EngineerCategoryCustom() {
		return b_T_Ufp_EngineerCategoryCustom;
	}
	public void setB_T_Ufp_EngineerCategoryCustom(
			B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategoryCustom) {
		this.b_T_Ufp_EngineerCategoryCustom = b_T_Ufp_EngineerCategoryCustom;
	}
	public String getIsupdate() {
		return isupdate;
	}
	public void setIsupdate(String isupdate) {
		this.isupdate = isupdate;
	}
}
